package com.mask.share.life.jk.hander;

import com.mask.share.life.jk.ExtInfo;
import com.mask.share.utils.ImageJavaUtils;
import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.opencv.core.Mat;

public abstract class BeforeAbsImageHandler implements ImageHandler {
    public abstract int getBeforeCount();

    @Override
    public ExtInfo handle(CircularFifoQueue<Mat> origImageBuffer, CircularFifoQueue<Mat> dealImageBuffer) {
        Mat currentImage = getCurrentImage(dealImageBuffer);
        Mat beforeImage = getBeforeImage(dealImageBuffer, getBeforeCount());
        if (beforeImage == null) {
            return null;
        }


        double similarity = ImageJavaUtils.calculateSimilarity(beforeImage, currentImage);

        ExtInfo extInfo = new ExtInfo();
        if (similarity < 0.9) {
            extInfo.setMatchFlag(true);
            extInfo.setSimilarity(similarity);
            extInfo.setOrigImage(getCurrentImage(origImageBuffer));
            extInfo.setDealImage(currentImage);
        } else {
            extInfo.setMatchFlag(false);
            extInfo.setSimilarity(similarity);
            extInfo.setOrigImage(getCurrentImage(origImageBuffer));
            extInfo.setDealImage(currentImage);
        }
        return extInfo;
    }
}

